home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 79 / maccd 79.iso / multimedial / GL Tron / Source / gltron / gui.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-26  |  7.9 KB  |  326 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #include "gltron.h"
  6.  
  7. void guiProjection(int x, int y) {
  8.   checkGLError("gui.c guiProj - start");
  9.   glMatrixMode(GL_PROJECTION);
  10.   glLoadIdentity();
  11.   /*glOrtho(0, 0, x, y, -1, 1); */
  12.   checkGLError("gui.c guiProj - proj");
  13.   glMatrixMode(GL_MODELVIEW);
  14.   glLoadIdentity();
  15.   glViewport(game->screen->vp_x, game->screen->vp_y,
  16.          x, y);
  17.   checkGLError("gui.c guiProj - end");
  18. }
  19.  
  20. void drawGuiBackground() {
  21.   checkGLError("gui background start");
  22.  
  23.   glClearColor(0.0, 0.0, 0.0, 0.0);
  24.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  25.  
  26.   rasonly(game->screen);
  27.  
  28.   if(game->settings->softwareRendering) {
  29.     glRasterPos2i(0, 0);
  30.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  31.     glDrawPixels(game->screen->vp_w, game->screen->vp_h,
  32.          GL_RGB, GL_UNSIGNED_BYTE,
  33.          game->screen->pixelGui);
  34.   } else {
  35.     glEnable(GL_TEXTURE_2D);
  36.     glBindTexture(GL_TEXTURE_2D, game->screen->textures[TEX_GUI]);
  37.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  38.  
  39.     glColor3f(1.0, 1.0, 1.0);
  40.     glBegin(GL_QUADS);
  41.  
  42.     glTexCoord2f(0.0, 0.0);
  43.     glVertex2f(0, 0);
  44.  
  45.     glTexCoord2f(1.0, 0.0);
  46.     glVertex2f(game->screen->vp_w, 0);
  47.  
  48.     glTexCoord2f(1.0, .75);
  49.     glVertex2f(game->screen->vp_w, game->screen->vp_h);
  50.  
  51.     glTexCoord2f(0.0, .75);
  52.     glVertex2f(0, game->screen->vp_h);
  53.  
  54.     glEnd();
  55.   }
  56. }
  57.  
  58. void drawGuiLogo() {
  59.   float pos[] = { 512 - 10 - 256, 384 - 64 };
  60.   float size[] = { 256, 64 };
  61.   float glpos = 64;
  62.   float glsize = 32;
  63.   float font_shift[] = { 0.5, 0.00 };
  64.  
  65.   checkGLError("gui logo start");
  66.   
  67.   rasonly(game->screen);
  68.  
  69.   pos[0] *= game->screen->vp_w / 512.0;
  70.   pos[1] *= game->screen->vp_h / 384.0;
  71.   size[0] *= game->screen->vp_w / 512.0;
  72.   size[1] *= game->screen->vp_h / 384.0;
  73.   glpos *= game->screen->vp_w / 512.0;
  74.   glsize *= game->screen->vp_w / 512.0;
  75.   
  76.   glEnable(GL_TEXTURE_2D);
  77.   glPushMatrix();
  78.   glTranslatef(pos[0] - glpos + glsize * font_shift[0], 
  79.            pos[1] + glsize * font_shift[1], 0);
  80.   glScalef(glsize, glsize, glsize);
  81.   glColor3f(0.2, 0.4, 0.8);
  82.   ftxRenderString(gameFtx, "gl", 2);
  83.   glPopMatrix();
  84.   
  85.   glBindTexture(GL_TEXTURE_2D, game->screen->textures[TEX_LOGO]);
  86.   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  87.  
  88.   glEnable(GL_BLEND);
  89.   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  90.  
  91.   glColor3f(1.0, 1.0, 1.0);
  92.   glBegin(GL_QUADS);
  93.  
  94.   glTexCoord2f(0.0, 0.0);
  95.   glVertex2f(pos[0], pos[1]);
  96.  
  97.   glTexCoord2f(1.0, 0.0);
  98.   glVertex2f(pos[0] + size[0], pos[1]);
  99.  
  100.   glTexCoord2f(1.0, 1.0);
  101.   glVertex2f(pos[0] + size[0], pos[1] + size[1]);
  102.  
  103.   glTexCoord2f(0.0, 1.0);
  104.   glVertex2f(pos[0], pos[1] + size[1]);
  105.  
  106.   glEnd();
  107.  
  108.   glDisable(GL_BLEND);
  109.  
  110.   checkGLError("gui background end");
  111. }
  112.   
  113. void displayGui() {
  114.   drawGuiBackground();
  115.   if(!game->settings->softwareRendering)
  116.     drawGuiLogo();
  117.   drawMenu(game->screen);
  118.  
  119.   SystemSwapBuffers();  
  120. }
  121.  
  122. void displayConfigure() {
  123.   char message[] = "Press a key for this action!";
  124.   drawGuiBackground();
  125.   if(!game->settings->softwareRendering)
  126.     drawGuiLogo();
  127.   drawMenu(game->screen);
  128.  
  129.   rasonly(game->screen);
  130.   glColor3f(1.0, 1.0, 1.0);
  131.   drawText(guiFtx, game->screen->vp_w / 6, 20,
  132.        game->screen->vp_w / (6.0 / 4.0 * strlen(message)), message);
  133.   SystemSwapBuffers();
  134. }
  135.  
  136. void idleGui() {
  137. #ifdef SOUND
  138.   soundIdle();
  139. #endif
  140.   SystemPostRedisplay(); /* animate menu */
  141. }
  142.  
  143. void keyboardConfigure(int key, int x, int y) {
  144.   *configureKeyEntry = key;
  145.   initMenuCaption(configureKeyMenu);
  146. #ifdef SOUND
  147.    playMenuFX(fx_action);
  148. #endif
  149.    drawGuiBackground();
  150.    if(!game->settings->softwareRendering)
  151.      drawGuiLogo();
  152.    drawMenu(game->screen);
  153.    rasonly(game->screen);
  154.    SystemSwapBuffers();
  155.   restoreCallbacks();
  156. }
  157.  
  158. void keyboardGui(int key, int x, int y) {
  159.   int i;
  160.   switch(key) {
  161.   case 27:
  162. #ifdef SOUND
  163.     if(game->settings->playEffects)
  164.       playMenuFX(fx_action);
  165. #endif
  166.     if(pCurrent->parent == NULL)
  167.       restoreCallbacks();
  168.     else
  169.       pCurrent = pCurrent->parent;
  170.     break;
  171.   case 13: case ' ':
  172. #ifdef SOUND
  173.     if(game->settings->playEffects)
  174.       playMenuFX(fx_action);
  175. #endif
  176.     menuAction(*(pCurrent->pEntries + pCurrent->iHighlight), MENU_ACTION);
  177.     break;
  178.   case SYSTEM_KEY_LEFT:
  179.     menuAction(*(pCurrent->pEntries + pCurrent->iHighlight), MENU_LEFT);
  180.     break;
  181.   case SYSTEM_KEY_RIGHT:
  182.     menuAction(*(pCurrent->pEntries + pCurrent->iHighlight), MENU_RIGHT);
  183.     break;
  184.   case 'q': SystemExit(); break;
  185.   case SYSTEM_KEY_DOWN:
  186. #ifdef SOUND
  187.     if(game->settings->playEffects)
  188.       playMenuFX(fx_highlight);
  189. #endif
  190.     pCurrent->iHighlight = (pCurrent->iHighlight + 1) % pCurrent->nEntries;
  191.     break;
  192.   case SYSTEM_KEY_UP:
  193. #ifdef SOUND
  194.     if(game->settings->playEffects)
  195.       playMenuFX(fx_highlight);
  196. #endif
  197.     pCurrent->iHighlight = (pCurrent->iHighlight - 1) % pCurrent->nEntries;
  198.     if(pCurrent->iHighlight < 0)
  199.       pCurrent->iHighlight = pCurrent->nEntries - 1;
  200.     break;
  201.   case SYSTEM_KEY_F11: doBmpScreenShot(); break;
  202.   case SYSTEM_KEY_F12: doScreenShot(); break;
  203.     /* debug code follows */
  204.   case 'l':
  205.     printf("%d entries:\n", pCurrent->nEntries);
  206.     for(i = 0; i < pCurrent->nEntries; i++)
  207.       printf("printing '%s' - %d entries\n",
  208.          ((Menu*)*(pCurrent->pEntries + i))->szName,
  209.          ((Menu*)*(pCurrent->pEntries + i))->nEntries);
  210.     break;
  211.   default: printf("got key %d\n", key);
  212.   }
  213.   SystemPostRedisplay();
  214. }
  215.  
  216. void initGui() {
  217.   menutime = SystemGetElapsedTime();
  218.   if(pCurrent == NULL) {
  219.     pCurrent = *pMenuList; /* erstes Menu ist RootMenu - Default pCurrent */
  220.     pCurrent->iHighlight = 0;
  221.   }
  222. }
  223.  
  224. void exitGui() {
  225.   glShadeModel( game->screen->shademodel );
  226. }
  227.  
  228. void initGLGui() {
  229.   glShadeModel(GL_FLAT);
  230.   glDisable(GL_BLEND);
  231.   glDisable(GL_LIGHTING);
  232.   glDisable(GL_DEPTH_TEST);
  233.   SystemPostRedisplay();
  234. }
  235.  
  236. static int current_highlight = -1;
  237.     
  238. void guiMouse(int buttons, int state, int x, int y) {
  239.   fprintf(stderr, "Mouse buttons: %d, State %d, Position (%d, %d)\n",
  240.       buttons, state, x, y); 
  241.  
  242.   /* fprintf(stderr, "testing for state == %d\n", SYSTEM_MOUSEPRESSED); */
  243.   if (state == SYSTEM_MOUSEPRESSED) {    
  244. #ifdef SOUND
  245.     if(game->settings->playEffects)
  246.       playMenuFX(fx_action);
  247. #endif    
  248.     menuAction(*(pCurrent->pEntries + pCurrent->iHighlight), MENU_ACTION);
  249.     SystemPostRedisplay();
  250.   }
  251. }
  252.  
  253. void guiMouseMotion(int mx, int my) {
  254.   /* fprintf(stderr, "Mouse motion at (%d, %d)\n", x, y); */
  255.  
  256.   /* I am using the calculation in drawMenu, perhaps
  257.      these values should be bound to Menu structure in the future. */
  258.  
  259.   /* TODO: this is cut-and-paste from menu.c: VERY UGLY */
  260.   int i;
  261.   int x, y, size, lineheight;
  262.   int hsize, vsize;
  263.   int maxw = 0;
  264.  
  265. #define MENU_TEXT_START_X 0.08
  266. #define MENU_TEXT_START_Y 0.40
  267.  
  268. #define MENU_WIDTH 0.80
  269. #define MENU_HEIGHT 0.40
  270.  
  271. #define MENU_TEXT_LINEHEIGHT 1.5
  272.     
  273.   gDisplay *d = game->screen; 
  274.     
  275.   x = (int) (d->vp_w * MENU_TEXT_START_X);
  276.   y = (int) (d->vp_h * MENU_TEXT_START_Y);
  277.  
  278.   /* transform mouse y-coordinate */
  279.  
  280.   my = d->vp_h - my;
  281.  
  282.   /* new stuff: calculate menu dimensions */
  283.   for(i = 0; i < pCurrent->nEntries; i++) {
  284.     int len;
  285.     len =  strlen(((Menu*)*(pCurrent->pEntries + i))->display.szCaption);
  286.     if(len > maxw)
  287.       maxw = len;
  288. }
  289.   /* adjust size so menu fits into MENU_WIDTH/HEIGHT */
  290.  
  291.   hsize = (int) ((float)d->vp_w * MENU_WIDTH / (float)maxw );
  292.   vsize = (int) ((float)d->vp_h * MENU_HEIGHT / 
  293.          ( (float)pCurrent->nEntries * MENU_TEXT_LINEHEIGHT));
  294.  
  295.   size = (hsize < vsize) ? hsize : vsize;
  296.  
  297.   lineheight = (int)( (float) size * MENU_TEXT_LINEHEIGHT);  
  298.  
  299.   for (i = 0; i < pCurrent->nEntries; i++) {
  300.     if (current_highlight != i)
  301.       if (my < y + lineheight && my > y ) {
  302.     current_highlight = i;
  303.  
  304. #ifdef SOUND
  305.     if(game->settings->playEffects)
  306.       playMenuFX(fx_highlight);
  307. #endif
  308.     pCurrent->iHighlight = i;
  309.     SystemPostRedisplay ();
  310.     break;    
  311.       }
  312.          
  313.     y -= lineheight;
  314.   }
  315. }
  316.  
  317. callbacks configureCallbacks = {
  318.   displayConfigure, idleGui, keyboardConfigure, initGui, exitGui, initGLGui,
  319.   NULL, NULL
  320. };
  321.  
  322. callbacks guiCallbacks = {
  323.   displayGui, idleGui, keyboardGui, initGui, exitGui, initGLGui, 
  324.   guiMouse, guiMouseMotion
  325. };
  326.